
package com.insigma.business.qrcode.common.template.area;


import com.insigma.business.qrcode.common.dto.BaseSearchRequest;
import com.insigma.business.qrcode.common.dto.ErrorCodeEnum;
import com.insigma.business.qrcode.common.template.BasePagingResponse;
import com.insigma.business.qrcode.common.util.CommonConstant;
import com.insigma.business.qrcode.common.util.ErrorContext;
import com.insigma.business.qrcode.common.vo.BaseResponse;
import com.insigma.business.qrcode.common.vo.RestfulResult;
import org.apache.commons.lang3.StringUtils;


public class ResponseUtil {

    
    public static <DTO> BaseResponse<DTO> setupSuccessResponse(final BaseResponse<DTO> baseResponse, final DTO resultObject){
        baseResponse.setSuccess(true);
        baseResponse.setDto(resultObject);

        return baseResponse;
    }

    
    public static <DTO> BasePagingResponse<DTO> setupSuccessPagingResponse(final BasePagingResponse<DTO> basePagingResponse ,
                                                                           final DTO resultObject,
                                                                           final BaseSearchRequest searchRequest){
        basePagingResponse.setPageNo(searchRequest.getPageNo());
        basePagingResponse.setPageSize(searchRequest.getPageSize());
        basePagingResponse.setSuccess(true);
        basePagingResponse.setDto(resultObject);

        return basePagingResponse;
    }

    
    public static <DTO> BasePagingResponse<DTO> buildSuccessPagingResponse(final BasePagingResponse<DTO> basePagingResponse ,
                                                                           final DTO resultObject,
                                                                           final BaseSearchRequest searchRequest,
                                                                           final long totalCount){
        basePagingResponse.setPageNo(searchRequest.getPageNo());
        basePagingResponse.setPageSize(searchRequest.getPageSize());
        basePagingResponse.setSuccess(true);
        basePagingResponse.setDto(resultObject);
        basePagingResponse.setTotalCount(totalCount);

        return basePagingResponse;
    }

    
    public static BaseResponse buildFailedResponse(final BaseResponse baseResponse,
                                                   final ErrorCodeEnum errorCodeEnum){
        baseResponse.setSuccess(false);
        baseResponse.setErrorCtx(new ErrorContext(errorCodeEnum.getCode(),errorCodeEnum.getDesc()));

        return baseResponse;
    }

    
    public static BaseResponse buildFailedResponse(final BaseResponse baseResponse,
                                                   final ErrorCodeEnum errorCodeEnum,
                                                   final String customDesc){
        baseResponse.setSuccess(false);
        baseResponse.setErrorCtx(new ErrorContext(errorCodeEnum.getCode(), StringUtils.isNotBlank(customDesc)?customDesc:errorCodeEnum.getDesc()));

        return baseResponse;
    }

    
    public static BaseResponse buildFailedResponse(final BaseResponse baseResponse,
                                                   final ScenarioCodeEnum scenarioCodeEnum,
                                                   final ErrorCodeEnum errorCodeEnum,
                                                   final String customDesc){
        baseResponse.setSuccess(false);
        baseResponse.setErrorCtx(new ErrorContext(CommonConstant.PRODUCTION_AND_APP_CODE, scenarioCodeEnum.getCode(), errorCodeEnum.getCode(), StringUtils.isNotBlank(customDesc)?customDesc:errorCodeEnum.getDesc()));

        return baseResponse;
    }


    public static RestfulResult buildFailedResponse(final RestfulResult baseResponse,
                                                   final ScenarioCodeEnum scenarioCodeEnum,
                                                   final ErrorCodeEnum errorCodeEnum,
                                                   final String customDesc){
        baseResponse.setSuccess(false);
        baseResponse.setErrorCtx(new ErrorContext(CommonConstant.PRODUCTION_AND_APP_CODE, scenarioCodeEnum.getCode(), errorCodeEnum.getCode(), StringUtils.isNotBlank(customDesc)?customDesc:errorCodeEnum.getDesc()));
        baseResponse.setCode(errorCodeEnum.getCode());
        baseResponse.setMsg(StringUtils.isNotBlank(customDesc)?customDesc:errorCodeEnum.getDesc());
        return baseResponse;
    }


}
